﻿namespace Webinator.Impls.WatiNImpl
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using Interfaces;
    using WatiN.Core;

    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    internal class WatiNHighlighter : IHighlighter
    {
        /// <summary>
        /// The core
        /// </summary>
        private readonly ICore _core;

        /// <summary>
        /// Initializes a new instance of the <see cref="WatiNHighlighter"/> class.
        /// </summary>
        /// <param name="core">
        /// The core.
        /// </param>
        public WatiNHighlighter(ICore core)
        {
            _core = core;
        }

        public void Highlight(Locator locator, bool reset)
        {
            Element element;
            try
            {
                element = (Element)_core.FindElement(locator);
            }
            catch (Exception)
            {
                return;
            }

            if (element == null) return;

            HighlightElement(element, reset);
        }

        public void HighlightElement(object element, bool reset = false)
        {
            if (!_core.Config.HighlightElements) return;
            try
            {
                ((Element)element).Highlight(!reset);
            }
            catch
            {
                // We don't really care if the highlight operation failed, so let's just move on
            }
        }
    }
}
